package com.wqtz.chechai.utils;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/12 10:38
 */

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;

public class PermissionUtil {

	public static final int WRITE_EXTERNAL_STORAGE = 1;
	public static final int CAMERA = 2;
	public static final int RECORD_AUDIO = 3;

	public static boolean requestFileWrite(Activity mActivity){
		if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
				!= PackageManager.PERMISSION_GRANTED) {
			//申请WRITE_EXTERNAL_STORAGE权限
			ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
					WRITE_EXTERNAL_STORAGE);
			return false;
		}else {
			return true;
		}
	}
	public static boolean requestCamera(Activity mActivity){
		if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.CAMERA)
				!= PackageManager.PERMISSION_GRANTED) {
			//CAMERA权限
			ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.CAMERA},
					CAMERA);
			return false;
		}else {
			return true;
		}
	}

	public static boolean requestAudio(Activity mActivity){
		if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.RECORD_AUDIO)
				!= PackageManager.PERMISSION_GRANTED) {
			//CAMERA权限
			ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.RECORD_AUDIO},
					RECORD_AUDIO);
			return false;
		}else {
			return true;
		}
	}


	public static boolean requestFileWrite(Fragment context){
		if (ContextCompat.checkSelfPermission(context.getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
				!= PackageManager.PERMISSION_GRANTED) {
			//申请WRITE_EXTERNAL_STORAGE权限
			context.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},WRITE_EXTERNAL_STORAGE);
			return false;
		}else {
			return true;
		}
	}

	public static boolean requestCamera(Fragment context){
		if (ContextCompat.checkSelfPermission(context.getActivity(), Manifest.permission.CAMERA)
				!= PackageManager.PERMISSION_GRANTED) {
			//申请CAMERA权限
			context.requestPermissions(new String[]{Manifest.permission.CAMERA},CAMERA);
			return false;
		}else {
			return true;
		}
	}
}
